/*
 * These files are distributed under the terms of the GNU GPLv2, the GNU General
 * Public version 2. See LICENSE.txt
 */
package fileCommodore.terminal;

import java.awt.*;
import java.io.*;
import java.util.*;

import javax.swing.*;

import de.mud.terminal.*;

/**
 * @author nado18@gmail.com, on Jul 20, 2007
 */
public class Terminal extends vt320
{
	private static final int BUFFER_SIZE = 512;
	
	private Process p;
	private InputStream bis;
	private InputStream bes;
	private OutputStream bos;
	
	private boolean exit = false;
	
	private Thread tOut = new Thread( new Runnable()
	{
		public void run()
		{
			byte[] buffer = new byte[BUFFER_SIZE];
			while (!shouldExit())
			{
				try
				{
					int len = bis.read( buffer );
					if (len > 0)
					{
						putString( new String( buffer, 0, len ) );
					}
				}
				catch (IOException ex)
				{
					ex.printStackTrace();
					die();
					return;
				}
			}
		}
	} );
	
	private Thread tErr = new Thread( new Runnable()
	{
		public void run()
		{
			byte[] buffer = new byte[BUFFER_SIZE];
			while (!shouldExit())
			{
				try
				{
					int len = bes.read( buffer );
					if (len > 0)
					{
						putString( new String( buffer, 0, len ) );
					}
				}
				catch (IOException ex)
				{
					ex.printStackTrace();
					die();
					return;
				}
			}
		}
	} );
	
	private Thread tProcess = new Thread( new Runnable()
	{
		public void run()
		{
			try
			{
				p.waitFor();
			}
			catch (InterruptedException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			die();
		}
	} );
	
	public void setProcess( Process process )
	{
		p = process;
		bis = new BufferedInputStream( p.getInputStream() );
		bes = new BufferedInputStream( p.getErrorStream() );
		bos = new BufferedOutputStream( p.getOutputStream() );
		
		tOut.start();
		tErr.start();
		tProcess.start();
	}
	
	private boolean shouldExit()
	{
		return exit;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see de.mud.terminal.vt320#write(byte[])
	 */
	@Override
	public void write( byte[] arg0 )
	{
		try
		{
			if (!shouldExit())
			{
				bos.write( arg0 );
				bos.flush();
			}
		}
		catch (IOException e)
		{
			die();
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void die()
	{
		exit = true;
		tOut.interrupt();
		tErr.interrupt();
	}
	
	public static void main( String[] args ) throws IOException
	{
		boolean showWindow = true;
		boolean closeOnExit = false;
		
		Terminal terminal = new Terminal();
		Font font = new Font( "Courier New", Font.PLAIN, 16 );
		SwingTerminal st = new SwingTerminal( terminal, font );
		st.setColorPrinting( true );
		st.setScrollbar( new JScrollBar() );
		
		JFrame f = new JFrame("File Commodore");
		f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		f.getContentPane().add( st );
		f.pack();
		f.setVisible( showWindow );
		
		Map<String, String> env = System.getenv();
		Vector<String> keys = new Vector<String>();
		for (String key : env.keySet())
		{
			if (!"TERM".equalsIgnoreCase( key ))
			{
				String value = env.get( key );
				keys.add( String.format( key, value ) );
			}
		}
		keys.add( "TERM=xterm-color" );
		String[] environment = new String[] {};
		environment = keys.toArray( environment );
		
		String cmd = "C:/Windows/System32/cmd.exe";
		Process p = Runtime.getRuntime().exec( cmd, environment );
		
		terminal.setProcess( p );
		
		try
		{
			p.waitFor();
		}
		catch (InterruptedException e)
		{
		}
		
		if( closeOnExit )
		{
			f.dispose();
		}
	}
}
